<?php

namespace App\Http\Controllers\v1;

use App\Model\CardLikeModel;
use App\Model\CardModel;
use App\Model\CommentModel;
use App\Model\FavoriteModel;
use App\Model\ImageModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;

class CardController extends BaseController
{
    //获取首页数据
    public function get(){
        $data = CardModel::where('is_delete','=',0)->hiident->paginate(10);
        foreach ($data as $k=>$v){
            $data[$k]['imgs']=ImageModel::where('cid','=',$v['id'])->get();
        }
        $list = $data->items();
        $res  = shuffle($list);
        if($res)
            return $this->success('首页帖子',$list,200);
        else
            return $this->error('首页帖子','',400);
    }

    //通过指定获取
    public function getById(Request $request){

       $id = $request->input('id');
        if($id){
             $card = CardModel::where('uid','=',$id)->get();
             $comment = CommentModel::where('cid','=',$id)->get();
             $data['card']=$card;
             $data['card']['imgs']=ImageModel::where(['cid'=>$card[0]['id']])->get();
             $data['comment']=$comment;
             return $this->success('帖子',$data,200);
        }else
        {
            return $this->error('非法请求','非法请求',500);
        }

    }

    public function test(){
        $token = '12345678';
        $uuid = 10;

    }

    //获取我的发帖
    public function getMyCard(Request $request){
        $token = $request->header('token');
        $uuid =  Cache::get($token)['uuid'];

        $data = CardModel::where(['uid'=>$uuid,'is_delete'=>0])->$this->get();
        foreach ($data as $k=>$v){
            $data[$k]['imgs']=ImageModel::where(['cid'=>$v['id']])->get();
        }
        return $this->success('我的发帖',$data,200);
    }

    public function getMyLike(Request $request){
        $token = $request->header('token');
       $uuid =  Cache::get($token)['uuid'];

        $uidlist = CardLikeModel::where(['uid'=>$uuid])->get();
        $data =[];
        foreach ($uidlist as $k=>$v){
            $data[$k]['card']=CardModel::find($v['cid']);
            $data[$k]['card']['imgs']=ImageModel::where('cid','=',$v['cid'])->get();
        }
        return $this->success('收藏',$data,200);

    }


    /**
     * 获取首页全部数据
     */
    public function getALLHomeDate(Request $request){
        $page = $request->input('page');
        if($page==null){
            return  $this->error('获取失败');
        }
        $res  = ( new  CardModel()) ->getALLHomeDate($page);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     * 获取首页我的大学数据
     * @param Request $request
     * @return CardController
     */
    public function getHomeMySchoolDate(Request $request){
        $page = $request->input('page');
        if($page==null){
            return  $this->error('获取失败');
        }
        $token = $request->get('token');
        $uuid =  Cache::get($token)['uuid'];

        $res  = ( new  CardModel()) ->getHomeMySchoolDate($uuid,$page);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     * 获取首页全我关注的大学
     * @param Request $request
     * @return CardController
     */
    public function getHomeMyFocusSchoolDate(Request $request){
        $page = $request->input('page');
        if($page==null){
            return  $this->error('获取失败');
        }
        $token = $request->get('token');
        $uuid =  Cache::get($token)['uuid'];

        $res  = ( new  CardModel()) ->getHomeMyFocusSchoolDate($uuid,$page);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

    /**
     * 获取首页最新数据
     * @return CardController
     */
    public function getHomeNewDate(Request $request){
        $page = $request->input('page');
        if($page==null){
            return  $this->error('获取失败');
        }
        $res  = ( new  CardModel()) ->getHomeNewDate($page);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }
    /**
     * 获取首页最热数据
     * @return CardController
     */
    public function getHomeHotDate(Request $request){
        $page = $request->input('page');
        if($page==null){
            return  $this->error('获取失败');
        }
        $res  = ( new  CardModel()) ->getHomeHotDate($page);

        if($res['code']){
            return $this -> success('成功！',$res['data']);
        }else{
            return $this -> error('失败！',$res['data']);
        }
    }

}
